<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Qh_QuestionLibraryRequest;
use App\Jobs\QuestionDeleteHelperJob;
use App\Models\Qh_Course;
use App\Models\Qh_Knowledge;
use App\Models\Qh_LitQuestion;
use App\Models\Qh_Question;
use App\Models\Qh_QuestionLibrary;
use DB;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class Qh_QuestionLibraryController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|object
     */
    public function index()
    {
        $list = Qh_QuestionLibrary::filter(\request()->all())
            ->orderByRaw('sort desc, id asc')
            ->paginate(\request('perPage', 10));
        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Qh_QuestionLibraryRequest $request
     * @return JsonResponse|object
     */
    public function store(Qh_QuestionLibraryRequest $request)
    {
        $params = $request->validated();
        if (!is_null($params['components'])) $params['components'] = json_encode($params['components']);
        $notice = Qh_QuestionLibrary::create($params);

        return $this->restful($notice);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function show(int $id)
    {
        return $this->restful();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Qh_QuestionLibraryRequest $request
     * @param int $id
     * @return JsonResponse|object
     */
    public function update(Qh_QuestionLibraryRequest $request, int $id)
    {
        $params = $request->validated();
        if (!empty($params['components'])) $params['components'] = json_encode($params['components']);
        $result = Qh_QuestionLibrary::where('id', $id)->update($params);

        return $this->restful($result);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function destroy(int $id)
    {
        DB::transaction(function () use ($id) {
            $result = Qh_QuestionLibrary::where('id', $id)->delete();

            // 删除学科
            Qh_Course::where('qlId', $id)->delete();

            // 删除知识点
            Qh_Knowledge::where('qlId', $id)->delete();

            // 删除试题
            $questions = DB::table('qh_questions')->where('qlId', $id)->get();
            foreach ($questions as $question) {
                QuestionDeleteHelperJob::dispatch($question);
            }

            // 删除小题
            Qh_LitQuestion::where('qlId', $id)->delete();
        });

        return $this->restful();
    }

    /**
     * 获取options
     * @return JsonResponse|object
     */
    public function getOptions() {
        $list = Qh_QuestionLibrary::selectRaw('id as value, name as label, province')
            ->orderByRaw('sort desc, province asc, id asc')
            ->get();

        return $this->restful($list);
    }
}
